<?php
/**
 * 助手函数
 */

//请求时间戳
function request_time(){
    return $_SERVER['REQUEST_TIME'] ?? time();
}
//请求的ip地址
function request_ip(){
    return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
}
//请求根网址
function request_domain($scheme = true){
    $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
    if($scheme){
        $https = $host['HTTPS'] ?? 'off';
        $https = ($https == 'on') ? 'https://' : 'http://';
        $host = $https.$host;
    }
    return $host;
}

//请求根网址
function jump($url){
    exit(header("Location: ".$url));
}
/**
 * 是否ajax
 * @return boolean
 */
function isAjax(){
    if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"] == 'xmlhttprequest')){
        return true;
    } else {
        return false;
    }
}

function layVerify($value){
    $verify = '';
    if(isset($value['not_null']) && $value['not_null']) $verify .= 'lay-verify="required" ';
    if(isset($value['not_null']) && $value['not_null']) $verify .= 'maxlength="'.$value['length'].'"';
    
    return $verify;
}

function mkdirs($dir, $baseDir = ''){
    if($baseDir){
        $rootPath = $baseDir;
        $arr = explode(DIRECTORY_SEPARATOR, $dir);
        foreach($arr as $k => $v){
            $rootPath .= DIRECTORY_SEPARATOR.$v;
            if(!file_exists($rootPath)){
                mkdir($rootPath);
            }
        }
        
    }elseif(isset($_SERVER['DOCUMENT_ROOT'])){
        $rootPath = $_SERVER['DOCUMENT_ROOT'];
        $arr = explode(DIRECTORY_SEPARATOR, $dir);
        foreach($arr as $k => $v){
            $rootPath .= DIRECTORY_SEPARATOR.$v;
            if(!file_exists($rootPath)){
                mkdir($rootPath);
            }
        }
    }
    
}